// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import ComponentBase from "./componentBase";
import Message, {MessageType} from "./Message";
import MessageCenter from "./MessageCenter";

const {ccclass, property} = cc._decorator;

@ccclass
export default class ManageBase extends ComponentBase {

    // 管理的消息接收者 type：array
    ReceiveList: ComponentBase[] = [];

    // 消息类型
    messageType: number;


    onLoad() {
        super.onLoad();
        console.log('ManageBase onLoad')

        // 设置当前管理类接收的消息类型
        this.messageType = this.SetMessageType();
        // 把管理类 加到消息中心
        MessageCenter.Managers.push(this);
    }

    // 子类重写后就为子类的 消息类型
    SetMessageType() {
        return MessageType.TYPE_UI;
    }

    // 注册消息中心
    RegisterReceiver(cb: ComponentBase) {
        // 添加 消息接收者
        this.ReceiveList.push(cb);
    }

    //接收消息 重写ComponentBase内的方法
    ReceiveMessage(message: Message) {
        super.ReceiveMessage(message);
        // 判断消息类型 大类
        console.log(`管理类收到消息`)
        console.log(this.ReceiveList)
        if (message.MessageType != this.messageType) {
            return;
        }
        // 向下广播 分发消息 在具体接收者内部 判断小类
        for (let cb of this.ReceiveList) {
            console.log(`向下广播 分发消息`)
            cb.ReceiveMessage(message)
        }
    }
}
